#! /bin/bash

set -xe
source pub.sh

PKCS_SIGN_FILE=pkcs.sign.tmp


# Build libavb
bash build_libavb.sh

# Build Demo - avb/libavb
cmake .
make


# Gen Private Key
[[ ! -e ${PRIV_KEY} ]] && openssl genrsa -out ${PRIV_KEY}

# Gen Public Key
[[ ! -e ${PUB_KEY} ]] && openssl rsa -in ${PRIV_KEY} -out ${PUB_KEY} -pubout

# Gen AVB Key - avbtool.py
$AVBTOOL extract_public_key --key $PRIV_KEY --output $AVB_PUB_KEY

# Sign (Using openssl)
openssl dgst -sha256 -sign ${PRIV_KEY} -out ${PKCS_SIGN_FILE} ${PLAIN_FILE_TO_SIGN_CFILE1}

# Verify (Using openssl)
openssl dgst -prverify ${PRIV_KEY} -sha256 -signature  ${PKCS_SIGN_FILE} ${PLAIN_FILE_TO_SIGN_CFILE1}

# Wait
echo "Wait for demo compilation to complete .."
wait

# Verify (Using Demo - avb/libavb)
./${DEMO_NAME} ${AVB_PUB_KEY} ${PLAIN_FILE_TO_SIGN_CFILE1} ${PKCS_SIGN_FILE}
echo "SHA256 - ${PLAIN_FILE_TO_SIGN_CFILE1}"
sha256sum ${PLAIN_FILE_TO_SIGN_CFILE1}
